{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业二 \n",
    "\n",
    "每行第一字母表示本人，其他字母他的朋友，找出有共同朋友的人，和共同朋友是谁，如何使用MapReduce来实现 20分\n",
    "\n",
    "```\n",
    "A B C D E F\n",
    "B A C D E\n",
    "C A B E\n",
    "D A B E\n",
    "E A B C D\n",
    "F A\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## friends.txt\n",
    "\n",
    "```\n",
    "A B C D E F\n",
    "B A C D E\n",
    "C A B E\n",
    "D A B E\n",
    "E A B C D\n",
    "F A\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## map.py\n",
    "\n",
    "```\n",
    "# 导入包\n",
    "import os\n",
    "import sys\n",
    "import re\n",
    "\n",
    "# 执行\n",
    "if __name__ == '__main__':\n",
    "    # 接收标准输入\n",
    "    handle = sys.stdin\n",
    "\n",
    "    # 循环取每一行\n",
    "    for line in handle:\n",
    "        if not line:\n",
    "            continue\n",
    "\n",
    "        # 把一行中的元素用空格分割\n",
    "        friend = line.strip().split(\" \")\n",
    "\n",
    "        owner = friend[0]\n",
    "        # 自己的朋友们\n",
    "        owner_friends = friend[1:]\n",
    "\n",
    "        # 共同的朋友\n",
    "        for i in range(0, len(owner_friends)):\n",
    "            for j in range(i + 1, len(owner_friends)):\n",
    "                # 打印 朋友一朋友二 自己\n",
    "                # 如 AB C\n",
    "                print(owner_friends[i] + owner_friends[j] + ' ' + owner)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## reduce.py\n",
    "\n",
    "```\n",
    "# 导入包\n",
    "import os\n",
    "import sys\n",
    "import re\n",
    "\n",
    "# 执行\n",
    "if __name__ == '__main__':\n",
    "    # 接收标准输入\n",
    "    handle = sys.stdin\n",
    "\n",
    "    # 空字典\n",
    "    word_dict = {}\n",
    "\n",
    "    # 循环取每一行\n",
    "    for line in handle:\n",
    "        if not line:\n",
    "            continue\n",
    "\n",
    "        # 把一行中的元素用空格分割\n",
    "        friend = line.strip().split(\" \")\n",
    "\n",
    "        # 查看获取的结果\n",
    "        # print(friend)\n",
    "\n",
    "        # host 为 上一结果的朋友一和朋友二组合成的键 设置为 key\n",
    "        host = line.strip().split(' ')[0]\n",
    "\n",
    "        # 判断 host 是否存在字典中，不在则创建一个 set 集合，把 host 的两个朋友添加进去设置为 key, 自己设置为值\n",
    "        if host not in word_dict.keys():\n",
    "            # 利用 set() 进行去重\n",
    "            frends_s = set()\n",
    "            frends_s.add(line.strip().split(' ')[1])\n",
    "            word_dict[host] = frends_s\n",
    "        else:\n",
    "            frends_s = word_dict[host]\n",
    "            frends_s.add(line.strip().split(' ')[1])\n",
    "\n",
    "    for j in word_dict:\n",
    "        print(j, ':'.join(word_dict[j]))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(base) ➜  作业二 git:(master) ✗ cat friends.txt | python map.py\n",
    "BC A\n",
    "BD A\n",
    "BE A\n",
    "BF A\n",
    "CD A\n",
    "CE A\n",
    "CF A\n",
    "DE A\n",
    "DF A\n",
    "EF A\n",
    "AC B\n",
    "AD B\n",
    "AE B\n",
    "CD B\n",
    "CE B\n",
    "DE B\n",
    "AB C\n",
    "AE C\n",
    "BE C\n",
    "AB D\n",
    "AE D\n",
    "BE D\n",
    "AB E\n",
    "AC E\n",
    "AD E\n",
    "BC E\n",
    "BD E\n",
    "CD E\n",
    "\n",
    "(base) ➜  作业二 git:(master) ✗ cat friends.txt | python map.py | python reduce.py\n",
    "BC {'A', 'E', 'BC'}\n",
    "BD {'BD', 'A', 'E'}\n",
    "BE {'A', 'C', 'BE', 'D'}\n",
    "BF {'BF', 'A'}\n",
    "CD {'A', 'E', 'CD', 'B'}\n",
    "CE {'A', 'CE', 'B'}\n",
    "CF {'A', 'CF'}\n",
    "DE {'DE', 'A', 'B'}\n",
    "DF {'A', 'DF'}\n",
    "EF {'A', 'EF'}\n",
    "AC {'AC', 'E', 'B'}\n",
    "AD {'E', 'AD', 'B'}\n",
    "AE {'C', 'D', 'B', 'AE'}\n",
    "AB {'E', 'C', 'AB', 'D'}\n",
    "\n",
    "(base) ➜  作业二 git:(master) ✗ cat friends.txt | python map.py | python reduce.py\n",
    "BC A:E\n",
    "BD A:E\n",
    "BE A:C:D\n",
    "BF A\n",
    "CD A:E:B\n",
    "CE A:B\n",
    "CF A\n",
    "DE A:B\n",
    "DF A\n",
    "EF A\n",
    "AC B:E\n",
    "AD B:E\n",
    "AE B:C:D\n",
    "AB C:E:D\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
